﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace GalaxyTranslator.MyMembership
{
	// Provides functions for a simple password encryption.
	public static class Sha1Helper
	{
		private static readonly SHA1 Sha1 = new SHA1Cng();

		// Compute a hash.
		public static string GetSha1Hash(string input)
		{
			byte[] data = Sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
			var sBuilder = new StringBuilder();
			for (int i = 0; i < data.Length; i++)
			{
				sBuilder.Append(data[i].ToString("x2"));
			}
			return sBuilder.ToString();
		}

		// Verify a hash against a string. 
		public static bool VerifySha1Hash(string input, string hash)
		{
			string hashOfInput = GetSha1Hash(input);		// Hash the input. 
			StringComparer comparer = StringComparer.OrdinalIgnoreCase;
			return comparer.Compare(hashOfInput, hash) == 0;
			
		}
	}
}